/*
 * $Id$
 * $HeadURL$
 *
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jettyrunner.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Andrei Solntsev
 * @version $Revision: $ $Date: $ $Author: $
 */
public class Utils
{
	protected static final String CLASS_VERSION = "$Revision: 1.2 $ $Author: asolntsev $";

	public static Properties loadProperties(File properties) throws IOException
	{
		final FileInputStream is = new FileInputStream(properties);
		try
		{
			Properties p = new Properties();
			p.load(is);
			return p;
		}
		finally
		{
			is.close();
		}
	}

	public static String arrayAsString(Object[] o)
	{
		StringBuilder sb = new StringBuilder("{");
		for (int i=0; i<o.length; i++)
		{
			if (i > 0)
				sb.append(", ");
			sb.append(o[i]);
		}
		return sb.append("}").toString();
	}

	public static String getProjectName(String cp)
	{
		String separator = "[\\/\\\\]";
		return cp.replaceFirst(".*"+separator+"(.*)"+separator+"target"+separator+"classes"+separator+"?", "$1");
	}

	public static boolean isSameJar(String projectName, String jarName)
	{
		return jarName.matches(projectName + "[-\\d\\.]*(-SNAPSHOT)?\\.jar");
	}

	private static final String RE_FILE_NAME = "([^\\.]*)\\.(.*)";
	public static String getFileName(String sNameWithExtension)
	{
		return sNameWithExtension.replaceFirst(RE_FILE_NAME, "$1");
	}

	public static String getFileExtension(String sNameWithExtension)
	{
		return sNameWithExtension.replaceFirst(RE_FILE_NAME, "$2");
	}
}
